package com.tudouji.project.farmer.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.common.utils.sql.SqlUtil;
import com.tudouji.framework.web.page.PageDomain;
import com.tudouji.framework.web.page.TableSupport;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.ISalespermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.farmer.mapper.FarmerinfoMapper;
import com.tudouji.project.farmer.domain.Farmerinfo;
import com.tudouji.project.farmer.service.IFarmerinfoService;

/**
 * 种植户档案Service业务层处理
 * 
 * @author tudouji
 * @date 2021-04-23
 */
@Service
public class FarmerinfoServiceImpl implements IFarmerinfoService
{
    @Autowired
    private FarmerinfoMapper farmerinfoMapper;
    @Autowired
    private ISalespermissionService permissionService;

    /**
     * 查询种植户档案
     * 
     * @param guid 种植户档案ID
     * @return 种植户档案
     */
    @Override
    public Farmerinfo selectFarmerinfoById(String guid)
    {
        return farmerinfoMapper.selectFarmerinfoById(guid);
    }

    /**
     * 查询种植户档案列表
     * 
     * @param farmerinfo 种植户档案
     * @return 种植户档案
     */
    @Override
    public List<Farmerinfo> selectFarmerinfoList(Farmerinfo farmerinfo, String userId) throws Exception
    {
        List<Salespermission> permissions = permissionService.getPermByScAndFunc(userId, "24", "4");
        int flag = 0;
        if (permissions.size() > 0) {
            flag = 1;
        } else {
            permissions = permissionService.getPermByScAndFunc(userId, "24", "2");
            if (permissions.size() > 0) {
                String[] departids = new String[permissions.size()];
                for (int i = 0; i < permissions.size(); i++) {
                    departids[i] = permissions.get(i).getDepartid();
                }
                farmerinfo.setDeptids(departids);
                flag = 1;
            }
        }
        if(flag==1){
            startPage();
            return farmerinfoMapper.selectFarmerinfoList(farmerinfo);
        }
        return new ArrayList<Farmerinfo>();
    }

    /**
     * 查询种植户档案列表
     *
     * @param farmerinfo 种植户档案
     * @return 种植户档案
     */
    @Override
    public List<Farmerinfo> export(Farmerinfo farmerinfo, String userId) throws Exception
    {
        List<Salespermission> permissions = permissionService.getPermByScAndFunc(userId, "24", "4");
        int flag = 0;
        if (permissions.size() > 0) {
            flag = 1;
        } else {
            permissions = permissionService.getPermByScAndFunc(userId, "24", "2");
            if (permissions.size() > 0) {
                String[] departids = new String[permissions.size()];
                for (int i = 0; i < permissions.size(); i++) {
                    departids[i] = permissions.get(i).getDepartid();
                }
                farmerinfo.setDeptids(departids);
                flag = 1;
            }
        }
        if(flag==1){
            return farmerinfoMapper.selectFarmerinfoList(farmerinfo);
        }
        return new ArrayList<Farmerinfo>();
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
    /**
     * 新增种植户档案
     * 
     * @param farmerinfo 种植户档案
     * @return 结果
     */
    @Override
    public int insertFarmerinfo(Farmerinfo farmerinfo)
    {
        return farmerinfoMapper.insertFarmerinfo(farmerinfo);
    }

    /**
     * 修改种植户档案
     * 
     * @param farmerinfo 种植户档案
     * @return 结果
     */
    @Override
    public int updateFarmerinfo(Farmerinfo farmerinfo)
    {
        return farmerinfoMapper.updateFarmerinfo(farmerinfo);
    }

    /**
     * 批量删除种植户档案
     * 
     * @param guids 需要删除的种植户档案ID
     * @return 结果
     */
    @Override
    public int deleteFarmerinfoByIds(String[] guids)
    {
        return farmerinfoMapper.deleteFarmerinfoByIds(guids);
    }

    /**
     * 删除种植户档案信息
     * 
     * @param guid 种植户档案ID
     * @return 结果
     */
    @Override
    public int deleteFarmerinfoById(String guid)
    {
        return farmerinfoMapper.deleteFarmerinfoById(guid);
    }

    public List<Farmerinfo> getHasPermissionFarmers(String tuserId) throws Exception{
        List<Farmerinfo> list=new ArrayList<>();
        Farmerinfo info=new Farmerinfo();
        List<Salespermission> permissions = permissionService.getPermByScAndFunc(tuserId, "24", "4");
        int flag = 0;
        if (permissions.size() > 0) {
            flag = 1;
        } else {
            permissions = permissionService.getPermByScAndFunc(tuserId, "24", "2");
            if (permissions.size() > 0) {
                String[] departids = new String[permissions.size()];
                for (int i = 0; i < permissions.size(); i++) {
                    departids[i] = permissions.get(i).getDepartid();
                }
                info.setDeptids(departids);
                flag = 1;
            }
        }
        if(flag==1){
            list=farmerinfoMapper.findFarmers(info);
        }
        return list;
    }

    public List<Farmerinfo> getAll(Farmerinfo farmerinfo, String tuserId){
        return farmerinfoMapper.findFarmers(farmerinfo);
    }
}
